using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Xml;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace DictCreator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            TextWriter w = new StreamWriter("test.out");
            
            /*System.Data.DataSet myDs = new DataSet();
            FileStream myFs =  new FileStream
                ("OED1.xml", FileMode.Open, FileAccess.Read);
            // Apply the ReadXml(fileSteeam method) to read the file
            myDs.ReadXml(myFs);

            // Get a DataTable object from the first table of the DataSet
            DataTable myDt = myDs.Tables[0];
            // Display the values for each row
            foreach (DataRow r in myDt.Rows)
                w.WriteLine("{0} : {1} : ${2:f2}",
                	r["ProductId"], r["ProductName"], 
                	Double.Parse(r["UnitPrice"].ToString()));
               // listBox1.Items.Add(r["title"].ToString());
            myFs.Close();
         */
            XmlTextReader mr = new XmlTextReader("OED1.xml");
            bool flg = true;
            int errs = 0;
            while (flg)
            {
                try {
                        flg =mr.Read();
                        if (mr.Name == "tei-ms:orth")
                        {
                            mr.Read();
                            if (mr.Value.ToString()[1] != ' ')
                            w.Write("{0}", mr.Value);

                        }
                
                    }
                catch (Exception ee)
                {
                    //do nothing
                    errs++;
                    flg = true;
                }
             //   w.WriteLine("<{0}>", mr.Value);

                /*switch (mr.NodeType)
                {
                    case XmlNodeType.Element:
                        w.WriteLine("<{0}>", mr.Name);
                        break;
                    case XmlNodeType.Text:
                        w.WriteLine(mr.Value);
                        break;
                    case XmlNodeType.CDATA:
                        w.WriteLine("<![CDATA[{0}]]>", mr.Value);
                        break;
                    case XmlNodeType.ProcessingInstruction:
                        w.WriteLine("<?{0} {1}?>", mr.Name, mr.Value);
                        break;
                    case XmlNodeType.Comment:
                        w.WriteLine("<!--{0}-->", mr.Value);
                        break;
                    case XmlNodeType.XmlDeclaration:
                        w.WriteLine("<?xml version='1.0'?>");
                        break;
                    case XmlNodeType.Document:
                        break;
                    case XmlNodeType.DocumentType:
                        w.WriteLine("<!DOCTYPE {0} [{1}]", mr.Name, mr.Value);
                        break;
                    case XmlNodeType.EntityReference:
                        w.WriteLine(mr.Name);
                        break;
                    case XmlNodeType.EndElement:
                        w.WriteLine("</{0}>", mr.Name);
                        break;
                }*/
                        
                

        }
        w.Close();
        textBox1.AppendText("Done with " + errs.ToString() + "errors\n");
    }

        private void button2_Click(object sender, EventArgs e)
        {
            TextReader rdr = new StreamReader("OED4.xml");
            TextWriter w = new StreamWriter("test.out");
            Regex r;
            // Create a Regex object and define its regular expression.
            r = new Regex(@"<tei-ms:orth>"); 
            string s;
            while ((s = rdr.ReadLine()) != null)
            {
                s=s.Trim();

                if (r.IsMatch(s))
                {
                    s = s.Remove(0,13);
                    s = s.Remove(s.Length-14);
                    w.WriteLine(s);
                }
            }
            w.Close();
            textBox1.AppendText("Done ");
        }   
    }
}